package com.hctforgreen.greenservice.ui.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
/**
 * TextView在android4.1和4.1.1上报ArrayIndexOutOfBoundsException的解决方案，
 * 
 * 原因：当TextView显示的表情恰好要被换行符截断的时候，会报异常（目前只在4.1和4.1.1上出现）
 * @author Tallen Zhai
 *
 */
public class PatchedTextView extends TextView {
	public PatchedTextView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public PatchedTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public PatchedTextView(Context context) {
		super(context);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		try {
			super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		} catch (ArrayIndexOutOfBoundsException e) {
			setText(getText().toString());
			super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		}
	}

	@Override
	public void setGravity(int gravity) {
		try {
			super.setGravity(gravity);
		} catch (ArrayIndexOutOfBoundsException e) {
			setText(getText().toString());
			super.setGravity(gravity);
		}
	}

	@Override
	public void setText(CharSequence text, BufferType type) {
		try {
			super.setText(text, type);
		} catch (ArrayIndexOutOfBoundsException e) {
			setText(text.toString());
		}
	}
}